package com.unionfetch.content_home.ui.view

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.animation.addListener
import com.unionfetch.content_home.HomeConstants
import com.unionfetch.content_home.R
import com.unionfetch.content_home.bean.AccountCategory
import com.unionfetch.content_home.bean.TradeAccountModel
import com.unionfetch.content_home.bean.TradeAccountStatus
import com.unionfetch.content_home.bean.getNameByType
import com.unionftech.common.R.string
import com.unionftech.common.common.bean.Platform
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.ToastUtils
import com.unionftech.common.utils.Utils
import com.unionftech.common.utils.formatAsMoney
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale


class AccountCardView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(context, attrs, defStyleAttr) {
    private val mContext = context
    private val mIvShowAccountDesc by lazy { findViewById<ImageView>(R.id.iv_show_account_desc) }
    private val mIvModifyLever by lazy { findViewById<ImageView>(R.id.iv_modify_lever_in_card) }
    private val mTvLever by lazy { findViewById<TextView>(R.id.tv_lever_in_card) }
    private val mTvLeverLabel by lazy { findViewById<TextView>(R.id.tv_lever_label_in_card) }
    private val mTvBalance by lazy { findViewById<TextView>(R.id.tv_balance) }
    private val mTvBalanceLabel by lazy { findViewById<TextView>(R.id.tv_balance_label) }
    private val mTvAccountType by lazy { findViewById<TextView>(R.id.tv_account_type) }
    private val mTvAccountTypeLabel by lazy { findViewById<TextView>(R.id.tv_account_type_label) }
    private val mTvServerName by lazy { findViewById<TextView>(R.id.tv_server_name) }
    private val mTvServerLabel by lazy { findViewById<TextView>(R.id.tv_server_label) }
    private val mLlAccountDesc by lazy { findViewById<LinearLayout>(R.id.ll_account_desc) }
    private val mTvAssetUnit by lazy { findViewById<TextView>(R.id.tv_asset_unit) }
    private val mTvAsset by lazy { findViewById<TextView>(R.id.tv_asset) }
    private val mIvCopyTradeNo by lazy { findViewById<ImageView>(R.id.iv_copy_trade_no) }
    private val mTvTradeNo by lazy { findViewById<TextView>(R.id.tv_trade_no) }
    private val mTvRemainderDays by lazy { findViewById<TextView>(R.id.tv_remainder_days) }
    private val mIvGotoDeposit by lazy { findViewById<ImageView>(R.id.iv_goto_deposit) }
    private val mTvGotoDeposit by lazy { findViewById<TextView>(R.id.tv_goto_deposit) }
    private val mLlDepositContainer by lazy { findViewById<LinearLayout>(R.id.ll_deposit_container) }
    private val mTvSecondLabel by lazy { findViewById<TextView>(R.id.tv_second_label) }
    private val mTvFirstLabel by lazy { findViewById<TextView>(R.id.tv_first_label) }
    private var mCurrentShowDesc = false

    init {
        LayoutInflater.from(mContext).inflate(R.layout.layout_wv_account_info_card_view, this, true)
        initLanguageTextForLabel()
        setListener()
    }

    private fun setListener() {
        mIvShowAccountDesc.setOnClickListener {
            actionExpandOrHideAnim()
        }
        mIvCopyTradeNo.setOnClickListener {
            val tradeNo = mTvTradeNo.text.toString()
            Utils.copyText(mContext, tradeNo)
            ToastUtils.longToast(DynamicResourceManager.getString(string.trade_no_copied))
        }
    }

    private fun actionExpandOrHideAnim() {
        if (!mCurrentShowDesc) {
            mLlAccountDesc.visibility = VISIBLE
            mLlAccountDesc.measure(0, 0)
        }
        val layoutParams = mLlAccountDesc.layoutParams
        val animator: ValueAnimator = if (mCurrentShowDesc) {
            ValueAnimator.ofInt(mLlAccountDesc.measuredHeight, 0)
        } else {
            ValueAnimator.ofInt(0, mLlAccountDesc.measuredHeight)
        }
        animator.addUpdateListener { animation ->
            layoutParams.height = (animation.animatedValue as Int)
            mLlAccountDesc.requestLayout()
        }
        animator.addListener(onEnd = {
            if (mCurrentShowDesc) {
                mLlAccountDesc.visibility = GONE
                mIvShowAccountDesc.setImageResource(R.drawable.ic_chevron_down)
            } else {
                mLlAccountDesc.visibility = VISIBLE
                mIvShowAccountDesc.setImageResource(R.drawable.ic_chevron_up)
            }
            mCurrentShowDesc = !mCurrentShowDesc
        })
        animator.duration = 300
        animator.start()
    }

    private fun initLanguageTextForLabel() {
        mTvGotoDeposit.text = DynamicResourceManager.getString(string.deposit)
        mTvServerLabel.text = DynamicResourceManager.getString(string.server)
        mTvAccountTypeLabel.text = DynamicResourceManager.getString(string.trading_account_type)
        mTvBalanceLabel.text = DynamicResourceManager.getString(string.balance)
        mTvLeverLabel.text = DynamicResourceManager.getString(string.lever)
    }

    @SuppressLint("SetTextI18n")
    fun setData(model: TradeAccountModel) {
        if (model.platform == Platform.MT4.platform) {
            mTvFirstLabel.text = Platform.MT4.showName
            mTvFirstLabel.setBackgroundResource(R.drawable.shape_account_mt4_label_bg)
        } else if (model.platform == Platform.MT5.platform) {
            mTvFirstLabel.text = Platform.MT5.showName
            mTvFirstLabel.setBackgroundResource(R.drawable.shape_account_mt5_label_bg)
        }
        if (model.type == AccountCategory.LIVE) {
            mTvSecondLabel.visibility = VISIBLE
            mTvSecondLabel.text = model.status.showName
            mLlDepositContainer.visibility = VISIBLE
            mTvRemainderDays.visibility = GONE
            if (model.status == TradeAccountStatus.RUNNING
                || model.status == TradeAccountStatus.READ_ONLY
            ) {
                mTvGotoDeposit.isEnabled = true
                mIvGotoDeposit.isEnabled = true
            } else {
                mTvGotoDeposit.isEnabled = false
                mIvGotoDeposit.isEnabled = false
            }
        } else {
            mTvSecondLabel.visibility = GONE
            mLlDepositContainer.visibility = GONE
            mTvRemainderDays.visibility = VISIBLE
            val str = DynamicResourceManager.getString(string.remainder_much_days)
            mTvRemainderDays.text = str.replace("<ph1></ph1>", model.validity.toString())
        }
        mTvTradeNo.text = model.tradeNo
        mTvAssetUnit.text = model.currency
        mTvServerName.text = model.serverName
        if (model.assets == Double.MIN_VALUE) {
            mTvAsset.text = "0.00"
        } else {
            mTvAsset.text = model.assets.formatAsMoney()
        }
        if (model.accountType == Int.MIN_VALUE) {
            mTvAccountType.text = "-"
        } else {
            mTvAccountType.text = model.accountType.getNameByType(mContext)
        }
        if (model.balance == Double.MIN_VALUE) {
            mTvBalance.text = "-"
        } else {
            mTvBalance.text = model.balance.formatAsMoney()
        }
        if (model.leverage == Int.MIN_VALUE) {
            mTvLever.text = "1:-"
            mIvModifyLever.setImageResource(R.drawable.ic_enable_edit)
        } else {
            mTvLever.text = "1:${model.leverage}"
            mIvModifyLever.setImageResource(R.drawable.ic_modify_lever)
        }
    }
}